from PyQt5.QtWidgets import QAction, QMenuBar, QFileDialog, QMenu
import App


class Action(QAction):
    window = None  # 主窗体
    # triggered = pyqtSignal(bool)  # 触发信号
    func = None  # 触发时运行的回调函数
    funcArg = False  # 为True时，在运行回调函数func时，会吧Action自己作为参数传递，否则不传递参数
    ignoreLayerEditing = False  # 无视图层编辑状态
    ignoreLayer = False  # 无视图层存在

    def __init__(self, name='', parent=QMenu, window=None):
        super().__init__(name, parent)
        self.triggered.connect(self.about)
        self.window = window
        self.__initUI()

    def __initUI(self):
        pass

    def about(self):
        if self.ignoreLayer:
            accept = True
        else:
            accept = False
            window = self.window
            if not window:
                return
            layer = window.currentLayer
            if not layer:
                return
            if self.ignoreLayerEditing:
                accept = True
            else:
                if layer.isEditing:
                    layer.aboutSave()
                elif layer.layerType != 'pixmap':
                    # 是否栅栏花
                    pass
                else:
                    accept = True
        if accept:
            if hasattr(self.func, "__call__"):
                if self.funcArg:
                    self.func(self)
                else:
                    self.func()


class MainWindowMenuBar(QMenuBar):
    window = None  # 主窗体

    def __init__(self, parent):
        super().__init__(parent)
        self.window = parent
        self.__initUI()

    def newAction(self, name='', parent=None):
        return Action(name, parent, self.window)

    def __initUI(self):
        font = self.font()
        font.setPixelSize(20)
        self.setFont(font)
        self.__initDocument()
        self.__initEdit()
        self.__initImage()
        self.__initFilter()
        self.__initMath()
        self.__initHelper()

    def __initDocument(self):
        doc = self.addMenu('文件')
        self.menu_doc = doc
        actionList = []

        action = self.newAction('打开', self)
        action.setStatusTip('打开文件')
        # action.func =App.openFile)
        action.func = App.openFile
        action.ignoreLayer = True
        actionList.append(action)

        action = self.newAction('新建', self)
        action.setStatusTip('新建文件')
        action.func = App.newImage
        action.ignoreLayer = True
        actionList.append(action)

        action = self.newAction('保存', self)
        action.setStatusTip('保存文件')
        action.func = App.saveFile
        actionList.append(action)

        action = self.newAction('另存为', self)
        action.setStatusTip('保存文件')
        actionList.append(action)

        # 添加选项
        for action in actionList:
            doc.addAction(action)

    def __initEdit(self):
        # 编辑栏
        menu = self.addMenu('编辑')
        actionList = []

        action = self.newAction('撤销', self)
        action.setStatusTip('撤销')
        actionList.append(action)
        action = self.newAction('前进', self)
        action.setStatusTip('前进')
        actionList.append(action)
        for action in actionList:  # 添加选项
            menu.addAction(action)
        menu.addSeparator()

        actionList.clear()
        action = self.newAction('填充', self)
        action.setStatusTip('填充')
        actionList.append(action)
        action = self.newAction('描边', self)
        action.setStatusTip('描边')
        actionList.append(action)
        for action in actionList:  # 添加选项
            menu.addAction(action)
        menu.addSeparator()

        actionList.clear()
        action = self.newAction('几何变换', self)
        action.setStatusTip('几何变换')
        actionList.append(action)
        action.func = App.transform
        for action in actionList:  # 添加选项
            menu.addAction(action)
        menu.addSeparator()

    def __initImage(self):
        # 图层栏
        menu = self.addMenu('图像')
        self.menu_layer = menu
        actionList = []

        # 模式
        submenu = menu.addMenu('模式')
        menu.menu_mode = submenu
        action = self.newAction('颜色空间', self)
        action.setStatusTip('颜色空间')
        actionList.append(action)
        for action in actionList:  # 添加选项
            submenu.addAction(action)

        menu.addSeparator()

        actionList.clear()
        submenu = menu.addMenu('调整')
        menu.menu_adjustment = submenu
        action = self.newAction('阈值', self)
        action.setStatusTip('阈值')
        action.func = App.threshold
        actionList.append(action)
        action = self.newAction('灰度图', self)
        action.setStatusTip('灰度图')
        action.func = App.toGrayImage
        actionList.append(action)
        action = self.newAction('反相', self)
        action.setStatusTip('反相')
        action.func = App.reversePhase
        actionList.append(action)
        action = self.newAction('对比度', self)
        action.setStatusTip('对比度')
        action.func = App.contrast
        actionList.append(action)
        action = self.newAction('直方图均衡', self)
        action.setStatusTip('直方图均衡')
        action.func = App.equalizeHist
        actionList.append(action)
        for action in actionList:  # 添加选项
            submenu.addAction(action)
        menu.addSeparator()
        # 统计
        actionList.clear()
        submenu = menu.addMenu('统计')
        action = self.newAction('直方图', self)
        action.setStatusTip('直方图')
        action.func = App.histogram
        actionList.append(action)
        for action in actionList:  # 添加选项
            submenu.addAction(action)

        # 其他
        actionList.clear()
        action = self.newAction('画布调整', self)
        action.setStatusTip('画布调整')
        for action in actionList:  # 添加选项
            menu.addAction(action)

    def __initFilter(self):
        menu = self.addMenu('滤镜')
        self.menu_filter = menu

    def __initMath(self):
        menu = self.addMenu('数学')
        self.menu_math = menu
        actionList = []

        action = self.newAction('高斯噪声', self)
        action.setStatusTip('高斯噪声')
        actionList.append(action)
        action.func = App.gasussNoise
        action = self.newAction('椒盐噪声', self)
        action.setStatusTip('椒盐噪声')
        actionList.append(action)
        action.func = App.spNoise
        for action in actionList:  # 添加选项
            menu.addAction(action)
        menu.addSeparator()

    def __initHelper(self):
        menu = self.addMenu('帮助')
        self.helper = menu
        actionList = []
        action = self.newAction('Welcome', self)
        action.setStatusTip('Welcome')
        action.ignoreLayer = True
        actionList.append(action)
        action.func = App.welcome
        for action in actionList:  # 添加选项
            menu.addAction(action)